package logic

import (
	"context"
	"usershop/good_srv/model/mysql"

	"usershop/good_srv/goods"
	"usershop/good_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *goods.GoodsListRequest) (*goods.CreateListResponse, error) {
	// todo: 获取商品

	//创建一个goods类型结构体
	g := mysql.Goods{}
	//list 是切片
	list, err := g.List(in.Keyword, int(in.Page), int(in.Size))

	//创建一个切片 用来循环
	var goodsList []*goods.GoodsItem
	//循环切片 list
	for _, v := range list {
		goodsList = append(goodsList, &goods.GoodsItem{
			GoodsID:     int64(v.ID),
			GoodsTitle:  v.GoodsTitle,
			GoodsPrice:  v.GoodsPrice,
			GoodsStock:  v.GoodsStock,
			GoodsStatus: v.GoodsStatus,
			GoodsCover:  v.GoodsCover,
		})
	}

	if err != nil {
		logx.Error("查询失败", err)
		return nil, err
	}

	return &goods.CreateListResponse{
		Item: goodsList,
	}, nil
}
